//
//  MainUserCenterViewController.m
//  ERP
//
//  Created by goldeney on 15/1/12.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MainUserCenterViewController.h"
#import "MainUserCenterViewCell.h"
#import "NSKeyedArchiverUserLogin.h"
#import "QueryMessageService.h"
#import "NSDictionary+DataFormat.h"

#import "DownloadViewController.h"
#import "AboutUsViewController.h"
#import "DeviceLockViewController.h"
#import "FeedbackViewController.h"

#import "CompanyCardViewController.h"
#import "ExitLoginService.h"
#import "UINavigationBar+Awesome.h"
#import "UIImage+Color.h"

@interface MainUserCenterViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *listInfoArray;
@property (nonatomic, assign) CGFloat tableHeaderHight;

@end

@implementation MainUserCenterViewController

- (id)init
{
    self = [super init];
    if (self) {
        //添加菜单权限获取注册
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMenuAuthorityService:)
//                                                     name:QueryMessageServiceKey object:nil];
    }
    return self;
}

- (void)dealloc
{
    //移除菜单权限获取注册
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:QueryMessageServiceKey object:nil];
    [self.navigationController.navigationBar lt_reset];
}

#pragma mark -
#pragma mark - NSNotificationCenter

//-(void)updateMenuAuthorityService:(NSNotification *)notification
//{
//    //获取消息信息
//    if ([[notification name] isEqualToString:QueryMessageServiceKey] && notification.object){
//        
//    }
//}

#pragma mark -
#pragma mark - 页面处理

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableHeaderHight = self.tableHeadImageView.image.size.height;
    self.tableView.tableHeaderView = self.tableHeadView;
    self.tableView.tableFooterView = self.tableFooterView;
    [self.tableView registerClass:[MainUserCenterViewCell class] forCellReuseIdentifier:@"MainUserCenterViewCell"];
    
    //获取用户登录信息
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    self.userNameLabel.text = userInfo.username && userInfo.username.length > 0 ? userInfo.username : @"未知用户";
    self.userPostLabel.text = userInfo.PostName && userInfo.PostName.length > 0 ? userInfo.PostName : @"未知职位";
    self.listInfoArray = @[
                           @[@[@"下载管理", @"查看已下载文件", @"main_user_down"],
                             @[@"安全中心", @"设备锁、密码修改", @"main_user_passwd"]],
                           
                           @[@[@"企业简码", @"企业简码信息", @"main_company_code"],
                              @[@"关于我们", @"软件信息及APP下载", @"main_user_about"]]
                           ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tableView.delegate = self;
    
    //去除黑色阴影线
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6){
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self scrollViewDidScroll:self.tableView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tableView.delegate = nil;
    //[self.navigationController.navigationBar lt_reset];  25baf5
}

#pragma mark- scroll delegate
#define NAVBAR_CHANGE_POINT 50

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset  = scrollView.contentOffset.y;
    
    UIColor * color = [UIColor colorWithRed:33/255.0 green:175/255.0 blue:244/255.0 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > NAVBAR_CHANGE_POINT) {
        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
    
    //头部图片拉大
    if (yOffset < 0) {
        CGRect f = self.tableHeadImageView.frame;
        f.origin.y = yOffset;
        f.size.height =  -yOffset + self.tableHeaderHight;
        f.size.height =  -yOffset + 168;
        self.tableHeadImageView.frame = f;
    }
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listInfoArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sectionArray = self.listInfoArray[section];
    return sectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainUserCenterViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainUserCenterViewCell" forIndexPath:indexPath];
    
    if(indexPath.row == 0) cell.lineView.hidden = YES;
    else cell.lineView.hidden = NO;
    
    if(indexPath.section == 1 && indexPath.row == 2)
        cell.phoneLabel.hidden = NO;
    else cell.phoneLabel.hidden = YES;
    
    NSArray *sectionArray = [self.listInfoArray objectAtIndex:indexPath.section];
    NSArray *infoArray = sectionArray[indexPath.row];
    cell.titleLable.text = [infoArray objectAtIndex:0];
    cell.introduceLable.text = [infoArray objectAtIndex:1];
    cell.iconView.image = [UIImage imageNamed:[infoArray objectAtIndex:2]];
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 12;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section < self.listInfoArray.count - 1) return 0.00001;
    return 12;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) { //下载管理
        DownloadViewController *viewController = [[DownloadViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.section == 0 && indexPath.row == 1) { //密码修改
        DeviceLockViewController *viewController = [[DeviceLockViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    }
//    else if (indexPath.section == 0 && indexPath.row == 2) { //意见反馈
//        FeedbackViewController *viewController = [[FeedbackViewController alloc] init];
//        [self.navigationController pushViewController:viewController animated:YES];
//        
//    }
    else if (indexPath.section == 1 && indexPath.row == 0) { //公司名片
        CompanyCardViewController *viewController = [[CompanyCardViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.section == 1 && indexPath.row == 1) { //关于我们
        AboutUsViewController *viewController = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.section == 1 && indexPath.row == 2) { //服务热线
        [self callAction:@""];
    }
}

#pragma mark
#pragma mark - UIAlertView

- (void)sendAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

#pragma mark - 拨打电话
- (void)callAction:(NSString*)phoneNumber
{
    if (!(phoneNumber && ![phoneNumber isEqualToString:@""])) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"未找到联系电话" delegate:self
                                            cancelButtonTitle:@"确定" otherButtonTitles:nil];
        alert.alertViewStyle=UIAlertViewStyleDefault;
        [alert show];
        return;
    } else {
        phoneNumber = [NSString stringWithFormat:@"tel://%@",phoneNumber];
    }
    
    //拨打电话(合法拨打电话方式)
    UIWebView*callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:phoneNumber];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    [self.view addSubview:callWebview];
}

#pragma mark Button Event

- (IBAction)userInfoSetEvent:(UIButton*)button
{
//    NTESUserInfoSettingViewController *vc = [[NTESUserInfoSettingViewController alloc] initWithNibName:nil bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark Button Event

- (IBAction)exitButtonEvent:(UIButton*)button
{
    [ExitLoginService exitAppUserLogin];
}


@end
